﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using HostsManager.Model;

namespace HostsManager.Controls
{
    internal partial class SectionControl : UserControl
    {
        internal delegate void DataChangedEventHandler(object sender);
        internal event DataChangedEventHandler DataChanged;


        internal delegate void RenameSectionEventHandler(Section section);
        internal event RenameSectionEventHandler RenameSection;

        internal delegate void RemoveSectionEventHandler(Section section);
        internal event RemoveSectionEventHandler RemoveSection;


        internal delegate void AddConfigurationEventHandler(Section section);
        internal event AddConfigurationEventHandler AddConfiguration;

        internal delegate void EditConfigurationEventHandler(Configuration configuration);
        internal event EditConfigurationEventHandler EditConfiguration;

        internal delegate void RenameConfigurationEventHandler(Configuration configuration);
        internal event RenameConfigurationEventHandler RenameConfiguration;

        internal delegate void DeleteConfigurationEventHandler(Configuration configuration);
        internal event DeleteConfigurationEventHandler DeleteConfiguration;

        private RadioButton selectedRadio = null;

        internal Section Section
        {
            get { return section; }
        }
        Section section;

        internal bool SectionEnabled
        {
            get { return checkBox1.Checked; }
            set { checkBox1.Checked = value; }
        }
        
        internal SectionControl(Section section)
        {
            InitializeComponent();

            this.section = section;
            this.checkBox1.Text = section.Name;

            RadioButton r;
            foreach(Configuration configuration in section.Configurations)
            {
                r = new RadioButton();
                r.CheckedChanged += new EventHandler(OnRadioButtonCheckedChanged);
                r.Tag = configuration;
                r.Visible = true;
                r.Text = configuration.Name;
                r.AutoSize = true;
                r.Checked = configuration.Enabled;
                r.ContextMenuStrip = mnuEditConfiguration;
                r.MouseDown += new MouseEventHandler(OnRadioButtonMouseDown);
                flowLayoutPanel1.Controls.Add(r);
            }
        }

        protected void OnRadioButtonMouseDown(object sender, MouseEventArgs e)
        {
            try
            {
                selectedRadio = (RadioButton)sender;
            }
            catch (Exception) { }
        }

        protected void OnRadioButtonCheckedChanged(object sender, EventArgs e)
        {
            RadioButton r = (RadioButton)sender;
            Configuration c = (Configuration)(r.Tag);

            if (c.Enabled != r.Checked)
            {
                c.Enabled = r.Checked;

                // When a radio goes checked another one goes unchecked (2 radiobutton_CheckedChanged will raise).
                // We want to raise DataChanged just 1 time (with the one being checked, for instance)
                if (r.Checked)
                    DataChanged(this);
            }
        }

        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            CheckBox control = ((CheckBox)sender);
            if (section.Enabled != control.Checked)
            {
                // Updates section status and raises an event.
                section.Enabled = control.Checked;

                // Radiobuttons can be modified only if the section is enabeld.
                flowLayoutPanel1.Enabled = control.Checked;

                OnDataChanged(this);
            }
        }

        protected virtual void OnDataChanged(object sender)
        {
            if (DataChanged != null)
                DataChanged(sender);
        }


        // ***********************************************************************************************
        // Configuration events **************************************************************************
        // ***********************************************************************************************

        private void addConfigurationMenu_Click(object sender, EventArgs e)
        {
            OnAddConfiguration(section);
        }

        private void renameConfigurationMenu_Click(object sender, EventArgs e)
        {
            Configuration configuration = null;

            try
            {
                configuration = (Configuration)(selectedRadio.Tag);
            }
            catch (Exception) { }

            if (configuration != null)
                OnRenameConfiguration(configuration);
        }

        private void editConfigurationMenu_Click(object sender, EventArgs e)
        {
            Configuration configuration = null;

            try
            {
                configuration = (Configuration)(selectedRadio.Tag);
            }
            catch (Exception) { }

            if (configuration != null)
                OnEditConfiguration(configuration);
        }

        private void removeConfigurationMenu_Click(object sender, EventArgs e)
        {
            Configuration configuration = null;

            try
            {
                configuration = (Configuration)(selectedRadio.Tag);
            }
            catch (Exception) { }

            if (configuration != null)
                OnDeleteConfiguration(configuration);
        }

        protected void OnAddConfiguration(Section section)
        {
            if (AddConfiguration != null)
                AddConfiguration(section);
        }

        protected void OnRenameConfiguration(Configuration configuration)
        {
            if (RenameConfiguration != null)
                RenameConfiguration(configuration);
        }

        protected void OnEditConfiguration(Configuration configuration)
        {
            if (EditConfiguration != null)
                EditConfiguration(configuration);
        }

        protected void OnDeleteConfiguration(Configuration configuration)
        {
            if (DeleteConfiguration != null)
                DeleteConfiguration(configuration);
        }

        // ***********************************************************************************************
        // Section events ********************************************************************************
        // ***********************************************************************************************

        private void renameSectionMenu_Click(object sender, EventArgs e)
        {
            OnRenameSection(section);
        }

        private void removeSectionMenu_Click(object sender, EventArgs e)
        {
            OnRemoveSection(section);
        }

        protected void OnRenameSection(Model.Section section)
        {
            if (RenameSection != null)
                RenameSection(section);
        }

        protected void OnRemoveSection(Model.Section section)
        {
            if (RemoveSection != null)
                RemoveSection(section);
        }
    }
}